package com.waijiaojun.tpo.service;

import com.aliyun.oss.ClientException;
import com.waijiaojun.tpo.entity.Question;
import com.waijiaojun.tpo.entity.QuestionCategory;
import com.waijiaojun.tpo.entity.QuestionCategoryRef;
import com.waijiaojun.tpo.repository.QuestionCategoryDao;
import com.waijiaojun.tpo.repository.QuestionDao;
import com.waijiaojun.tpo.rest.RestClientException;
import com.waijiaojun.tpo.utils.AliyunOssUtil;
import com.waijiaojun.tpo.utils.JsonUtil;
import net.sf.json.util.JSONUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by Peter on 2018/3/1.
 */
@Service
public class QuestionService {
    @Autowired
    private QuestionDao questionDao;

    @Autowired
    private QuestionCategoryDao questionCategoryDao;

    public List<Question> findByArticleId(Long articleId) {
        List<Question> list = questionDao.findByScenarioId(articleId);
        // 查询题目的分类
        List<QuestionCategoryRef> cateRefs = questionDao.findCategoryRefsByQuestionIds(list);

        for (Question question : list) {
            List<Long> cateIds = new ArrayList<>();
            for (QuestionCategoryRef ref : cateRefs) {
                if (question.getId().longValue() == ref.getQuestionId().longValue()) {
                    cateIds.add(ref.getQuestionCategoryId());
                }
                question.setQuestionCategoryIds(JsonUtil.toJson(cateIds));
            }
        }

        return list;
    }

    public void save(Question question) {
        if (question.getId() == null) {
            questionDao.insertSelective(question);
        } else {
            questionDao.update(question);
        }

        // 更新题目类型
        List<Long> cateIds = getCateIds(question);
        if (cateIds.size() > 0) {
            questionDao.deleteQuestionCategory(question.getId());
            questionDao.updateQuestionCategory(cateIds, question.getId());
        }
    }

    private List<Long> getCateIds(Question question) {
        List<Long> cateIds = new ArrayList<>();
        String str_ids = question.getQuestionCategoryIds();
        if (StringUtils.isNotBlank(str_ids)) {
            str_ids = str_ids.trim().replace("[", "").replace("]", "");
            if (StringUtils.isNotBlank(str_ids)) {
                String[] ids = str_ids.split(",");
                if (ids != null && ids.length >= 0) {
                    for (int i = 0; i < ids.length; i++) {
                        cateIds.add(Long.parseLong(ids[i]));
                    }

                }
            }
        }
        return cateIds;
    }

    public void delete(Long id) {
        Question question = questionDao.findById(id);
        questionDao.updateNumberForDelete(question.getScenario_id(), question.getNumber());
        questionDao.delete(id);
    }

    public String uploadAudio(Long questionId, MultipartFile file) {
        if (file == null || file.isEmpty()) {
            throw new RestClientException("文件无效：空文件");
        }
        String pathName = AliyunOssUtil.uploadQuestionAudioFile(file, questionId);
        Question question = new Question();
        question.setId(questionId);
        question.setAudio_url(pathName);
        questionDao.update(question);
        return question.getAudio_url();
    }

    public List<QuestionCategory> findAllQuestionCategoryList() {
        return questionCategoryDao.findAll();
    }
}
